//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include "clsinfo.h"
#include "marshal.h"
#include "sharecls.h"
#include "aura.h"

ECode RegsiterLocalClassInfo(
    /* [in]  */ CIClassInfo *pClassInfo)
{
    if (NULL == pClassInfo) {
        return E_INVALID_ARGUMENT;
    }

    return RegisterSharedClassInfo(pClassInfo);
}

ECode UnregisterClassInfo(
    /* [in]  */ CIClassInfo *pClassInfo)
{
    if (NULL == pClassInfo) {
        return E_INVALID_ARGUMENT;
    }
    return UnregisterSharedClassInfo(pClassInfo);
}

ECode GetLocalClassInfo(
    /* [in]  */ REMuid clsId,
    /* [out] */ CIClassInfo **ppClassInfo)
{
    if (NULL == ppClassInfo) {
        return E_INVALID_ARGUMENT;
    }

    return LookupSharedClassInfo(clsId, ppClassInfo);
}

ECode GetLocalClassEntry(
    /* [in]  */ REMuid clsId,
    /* [out] */ CIClassEntry **ppClassEntry)
{
    if (NULL == ppClassEntry) {
        return E_INVALID_ARGUMENT;
    }

    return LookupSharedClassEntry(clsId, ppClassEntry);
}

ECode GetRemoteClassEntry(
    /* [in]  */ CChannelPool * pChannelPool,
    /* [in]  */ REMuid clsId,
    /* [out] */ CIClassEntry **ppClassEntry)
{
    PVoid  pBuf    = NULL;
    UInt32 BufSize = MALLOC_SIZE;

    ECode   ec = NOERROR;
    UInt32  replyType;

    pBuf = (UInt32*)Aura_malloc((Aura_Int32)BufSize);
    if (NULL == pBuf) {
        return E_OUT_OF_MEMORY;
    }

    SET_REQUEST_TYPE(pBuf,Request_MetaData);
    memcpy((PVoid)BY_REFERENCE(pBuf,1),(PVoid)&clsId, sizeof(EMuid));

   ec = pChannelPool->WriteAndRead(pBuf, sizeof(EMuid)+sizeof(UInt32),
                                   pBuf, BufSize);
    if (FAILED(ec)) {
        goto ErrorExit;
    }

    GET_REPLY_TYPE(pBuf, replyType);
    assert((Reply_MetaData == replyType) \
           || (Reply_ECodeOnly == replyType));

    GET_REPLY_ECODE(pBuf,ec);
    if (FAILED(ec)) {
        ec = E_FAIL;
        goto ErrorExit;
    }
    else {
        ec = RegisterSharedClassInfo((CIClassInfo*)BY_REFERENCE(pBuf,2));
        if (FAILED(ec)) {
            goto ErrorExit;
        }

        ec = GetLocalClassEntry(clsId,ppClassEntry);
    }

ErrorExit:
    Aura_free(pBuf);
    return ec;
}
